package labox.innovation.gameserver.network.gameserverpackets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import labox.innovation.config.Config;
import labox.innovation.db.core.L2DatabaseFactory;

public class CharacterCountsPacket extends GameServerBasePacket {

	public CharacterCountsPacket(String account) {
		int characterCounts = 0;
		String sql = "select count(1) as character_count from characters where account_name = ? group by account_name";
		Connection con = null;
		PreparedStatement statement = null;
		try {
			con = L2DatabaseFactory.getInstance().getConnection();
			statement = con.prepareStatement(sql);
			statement.setString(1, account);
			ResultSet rs = statement.executeQuery();
			while (rs.next()) {
				characterCounts = rs.getInt("character_count");
			}
			rs.close();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				statement.close();
				con.close();
			} catch (Exception e) {
			}
		}

		writeByte(0x07);
		writeByte(Config.SERVER_ID); //-- 游戏服ID
		writeString(account); //-- 登陆账户
		writeByte(characterCounts); //-- 当前玩家在游戏服上创建角色数量
	}

	@Override
	public byte[] getContent() throws IOException {
		return getBytes();
	}
}
